<<<<<<< .mine
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.awpspace.caromini;

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

/**
 * @author phamk_000
 */
public class OneGameScreen extends GameCanvas implements CommandListener {

	/**
	 * constructor
	 */
	public static final int START = 1;
	public static final int WARNING = 2;
	public static final int WINGAME = 3;
	public static final int LOSEGAME = 4;

	private BoardCaro caro;
	private int widthCell = 25;
	private int heightCell = 25;
	private boolean gameOver = false;
	private Algorithm al;
	private CaroMini game;

	Image X, O, imExpandUp, imExpandDown, imMoveUp, imMoveDown, imClearUp,
			imClearDown, imBackUp, imBackDown, imExpand, imrow, imcol, imOkUp,
			imOkDown, imHand, imYourTurn, imMobileTurn, imYouWin, imMobileWin;
	MySprite yourTurn, mobileTurn;
	Button expand, move, clear, back, ok;
	boolean isExpand = false;
	boolean allowMove = false;
	int count = 0;
	Board board;
	int currentX, currentY;
	Hand hand;
	int curX, curY;
	boolean isTurn = true;
	int win = 0;
	Vector current = new Vector();

	public OneGameScreen(CaroMini g) {
		super(true);
		setFullScreenMode(true);
		game = g;
		caro = BoardCaro.getInstance();
		caro.reset();
		al = new Algorithm();
		try {
			setCommandListener(this);
			addCommand(new Command("Exit", Command.BACK, 1));
		} catch (Exception e) {
			e.printStackTrace();
		}
		load();
		board = new Board(imrow, imcol, 120 - 250, 160 - 250);
	}

	private void load() {
		X = loadImage("/x.png");
		O = loadImage("/o.png");
		imMoveUp = loadImage("/moveup.png");
		imMoveDown = loadImage("/movedown.png");
		imClearUp = loadImage("/clearup.png");
		imClearDown = loadImage("/cleardown.png");
		imBackUp = loadImage("/resetup.png");
		imBackDown = loadImage("/resetdown.png");
		imExpand = loadImage("/expand.png");
		imrow = loadImage("/row.png");
		imcol = loadImage("/col.png");
		imYourTurn = loadImage("/yourturn.png");
		imMobileTurn = loadImage("/mobileturn.png");
		imOkUp = loadImage("/ok1.png");
		imOkDown = loadImage("/ok2.png");
		imHand = loadImage("/hand.png");
		imExpandUp = loadImage("/expandup.png");
		imExpandDown = loadImage("/expanddown.png");
		imYouWin = loadImage("/youwin.png");
		imMobileWin = loadImage("/mobilewin.png");

		expand = new Button(imExpandUp, imExpandDown, 0, 285);
		move = new Button(imMoveUp, imMoveDown, 42, 289);
		clear = new Button(imClearUp, imClearDown, 77, 289);
		back = new Button(imBackUp, imBackDown, 112, 289);
		ok = new Button(imOkUp, imOkDown, 190, 280);
		hand = new Hand(imHand);
		hand.update(120, 160);

		yourTurn = new MySprite(imYourTurn, 2, 1, 5, 5);
		mobileTurn = new MySprite(imMobileTurn, 2, 1, 130, 15);
		setListener();
		mobileTurn.nextFrame();
	}

	private void setListener() {
		expand.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				isExpand = !isExpand;
				repaint();
			}
		});

		move.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				isExpand = !isExpand;
				allowMove = !allowMove;
				expand.isPressed = false;
				repaint();
			}
		});
		clear.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				reset();
				repaint();
				win = 0;

			}
		});
		back.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				back();
				repaint();
			}
		});
		ok.setListener(new ButtonListener() {
			public void actionUp() {
				allowMove = !allowMove;
				repaint();
				super.actionUp();
			}
		});
	}

	private Image loadImage(String path) {
		return ImageLoader.getInstance().loadImage(path);
	}

	/**
	 * paint
	 */
	public void drawCell(int type, int x, int y, Graphics g) {
		int real_x = x * heightCell;
		int real_y = y * widthCell;
		int anchorX = 5;
		int anchorY = 0;
		if (type == 1) {
			g.setColor(255, 0, 0);
			g.drawString("X", real_x + anchorX, real_y + anchorY, 0);
		} else if (type == 2) {
			g.setColor(0, 0, 255);
			g.drawString("O", real_x + anchorX, real_y + anchorY, 0);
		}
	}

	public void drawBoard(Graphics g) {
		for (int i = 0; i < 10; i++) {
			g.drawImage(imrow, 0, 40 + heightCell * i, 0);
		}
		for (int i = 0; i < 10; i++) {
			g.drawImage(imcol, 20 + widthCell * i, 0, 0);
		}
	}

	public void paint(Graphics g) {
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, 240, 320);
		board.paint(g);
		board.drawValue(g, BoardCaro.getInstance().board, X, O);

		if (allowMove) {
			ok.draw(g);
			hand.draw(g);
		}
		if (isExpand) {
			g.drawImage(imExpand, 5, 320 - imExpand.getHeight() - 1, 0);
			move.draw(g);
			clear.draw(g);
			back.draw(g);
		}
		expand.draw(g);
		if (gameOver) {
			g.setColor(250, 70, 250);
			g.setFont(Font.getDefaultFont());
		}
		yourTurn.paint(g);
		mobileTurn.paint(g);

		if (win == 1)
			g.drawImage(imYouWin, 30, 200, 0);
		else if (win == 2)
			g.drawImage(imMobileWin, 5, 200, 0);
	}

	/**
	 * Called when the pointer is dragged.
	 */
	protected void pointerDragged(int x, int y) {
		if (allowMove) {
			int a, b;
			if (x - currentX < 0)
				a = -2;
			else
				a = 2;
			if (y - currentY < 0)
				b = -2;
			else
				b = 2;
			board.move(a, b);
			hand.update(x, y);
			repaint();
		}
	}

	/**
	 * Called when the pointer is pressed.
	 */
	protected void pointerPressed(int x, int y) {
		currentX = x;
		currentY = y;
		expand.actionDown(x, y);
		if (allowMove)
			ok.actionDown(x, y);
		if (isExpand) {
			move.actionDown(x, y);
			clear.actionDown(x, y);
			back.actionDown(x, y);

		}
		repaint();
	}

	/**
	 * Called when the pointer is released.
	 */
	protected void pointerReleased(int x, int y) {

		if (allowMove) {
			ok.actionUp(x, y);
			repaint();
			return;
		}
		if (expand.isPressed || move.isPressed || clear.isPressed
				|| back.isPressed) {
			expand.actionUp(x, y);
			move.actionUp(x, y);
			clear.actionUp(x, y);
			if (!gameOver)
				back.actionUp(x, y);
			repaint();
			return;
		}
		if (isExpand || gameOver) {
			return;
		}
		System.out.println(x + " " + y);
		if (isTurn) {

			int real_x = (x + Math.abs(board.x)) / widthCell;
			int real_y = (y + Math.abs(board.y)) / heightCell;
			System.out.println("Me: " + real_x + " " + real_y);

			if (caro.getValue(real_x, real_y) != 0) {
				warning();
				return;
			}
			caro.setValue(real_x, real_y, 1);
			al.setCurrentEnemy(real_x, real_y);
			current.addElement(new int[] { real_x, real_y });
			yourTurn.nextFrame();
			mobileTurn.nextFrame();
			repaint();
			if (al.isEnemyWin()) {
				gameOver = true;
				win = 1;
				isExpand = true;
				notifyYouWinGame();
				repaint();
				return;
			}
			isTurn = false;
			mobileTurn();

		}
		repaint();
	}

	private void notifyMobileWinGame() {

	}

	private void notifyYouWinGame() {

	}

	private void warning() {
		Alert warning = new Alert("Invalid move");
		warning.addCommand(new Command("OK", Command.OK, 1));
		warning.setCommandListener(new CommandListener() {

			public void commandAction(Command c, Displayable d) {
				game.getDisplay().setCurrent(OneGameScreen.this);
			}
		});
		game.switchDisplayable(warning, this);
	}

	private void back() {
		if (current.size() == 0)
			return;
		int[] a, b;
		System.out.println(current.size());
		a = (int[]) current.lastElement();
		current.removeElementAt(current.size() - 1);
		System.out.println(current.size() + "  " + a[0] + "|" + a[1]);
		b = (int[]) current.lastElement();
		current.removeElementAt(current.size() - 1);
		System.out.println(current.size() + "  " + b[0] + "|" + b[1]);
		caro.setValue(a[0], a[1], 0);
		caro.setValue(b[0], b[1], 0);
		repaint();
	}

	private Timer timer = new Timer();

	public void mobileTurn() {
		TimerTask task = new TimerTask() {
			public void run() {

				al.decision();
				current.addElement(new int[] { al.current_me_X, al.current_me_Y });
				isTurn = true;
				mobileTurn.nextFrame();
				yourTurn.nextFrame();
				if (al.isMeWin()) {
					// gameOverDescription = "0 is winner!";
					win = 2;
					isExpand = true;
					gameOver = true;
					repaint();
					notifyMobileWinGame();
					return;
				}
				repaint();
			}
		};
		timer.schedule(task, 1);

	}

	/**
	 * Called when action should be handled
	 */
	public void commandAction(Command command, Displayable displayable) {
		Alert warning = new Alert("Are you really quit!");
		warning.addCommand(new Command("OK", Command.OK, 1));
		warning.addCommand(new Command("Cancel", Command.CANCEL, 1));
		warning.setCommandListener(new CommandListener() {

			public void commandAction(Command c, Displayable d) {
				if (c.getLabel().equals("OK")) {
					game.getDisplay().setCurrent(new MenuGame(game));
				} else if (c.getLabel().equals("Cancel")) {
					game.getDisplay().setCurrent(OneGameScreen.this);
				}
			}
		});
		game.switchDisplayable(warning, this);
	}

	public void reset() {
		al.reset();
		caro.reset();
		gameOver = false;
	}

	public boolean inSprite(Sprite s, int x, int y) {
		return x > s.getX() && x < s.getX() + s.getWidth() && y > s.getY()
				&& y < s.getY() + s.getHeight();
	}
}
=======
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.awpspace.caromini;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

/**
 * @author phamk_000
 */
public class OneGameScreen extends GameCanvas implements CommandListener {

	/**
	 * constructor
	 */
	private BoardCaro caro;
	private final int WIDTH = 240;
	private final int HEIGHT = 240;
	private int widthCell = 25;
	private int heightCell = 25;
	private boolean gameOver = false;
	private Algorithm al;
	private CaroMini game;

	Image X, O, imExpandUp, imExpandDown, imMoveUp, imMoveDown, imClearUp,
			imClearDown, imBackUp, imBackDown, imExpand, imrow, imcol,
			imYourTurn, imMobileTurn;
	MySprite yourTurn, mobileTurn;
	Button expand, move, clear, back;
	boolean isExpand = false;
	boolean allowMove = false;
	Board board;
	int currentX, currentY;

	public OneGameScreen(CaroMini g) {
		super(true);
		setFullScreenMode(true);
		game = g;
		caro = BoardCaro.getInstance();
		caro.reset();
		al = new Algorithm();
		// widthCell = WIDTH / caro.COL;
		// heightCell = HEIGHT / caro.ROW;
		try {
			setCommandListener(this);
			addCommand(new Command("Exit", Command.EXIT, 1));
		} catch (Exception e) {
			e.printStackTrace();
		}
		load();
		board = new Board(imrow, imcol, 120 - 250, 160 - 250);
	}

	private void load() {
		X = loadImage("/x.png");
		O = loadImage("/o.png");
		imMoveUp = loadImage("/moveup.png");
		imMoveDown = loadImage("/movedown.png");
		imClearUp = loadImage("/clearup.png");
		imClearDown = loadImage("/cleardown.png");
		imBackUp = loadImage("/resetup.png");
		imBackDown = loadImage("/resetdown.png");
		imExpand = loadImage("/expand.png");
		imrow = loadImage("/row.png");
		imcol = loadImage("/col.png");
		imYourTurn = loadImage("/yourturn.png");
		imMobileTurn = loadImage("/mobileturn.png");

		imExpandUp = loadImage("/expandup.png");
		imExpandDown = loadImage("/expanddown.png");

		expand = new Button(imExpandUp, imExpandDown, 0, 285);
		move = new Button(imMoveUp, imMoveDown, 42, 289);
		clear = new Button(imClearUp, imClearDown, 77, 289);
		back = new Button(imBackUp, imBackDown, 112, 289);

		setListener();
	}

	private void setListener() {
		expand.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				isExpand = !isExpand;
				repaint();
			}
		});
		move.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				allowMove = !allowMove;
			}
		});
		clear.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				reset();
				repaint();
			}
		});
		back.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
			}
		});
	}

	private Image loadImage(String path) {
		return ImageLoader.getInstance().loadImage(path);
	}

	/**
	 * paint
	 */
	public void drawCell(int type, int x, int y, Graphics g) {
		int real_x = x * heightCell;
		int real_y = y * widthCell;
		int anchorX = 5;
		int anchorY = 0;
		if (type == 1) {
			g.setColor(255, 0, 0);
			g.drawString("X", real_x + anchorX, real_y + anchorY, 0);
		} else if (type == 2) {
			g.setColor(0, 0, 255);
			g.drawString("O", real_x + anchorX, real_y + anchorY, 0);
		}
	}

	public void drawBoard(Graphics g) {
		for (int i = 0; i < 10; i++) {
			g.drawImage(imrow, 0, 40 + heightCell * i, 0);
		}
		for (int i = 0; i < 10; i++) {
			g.drawImage(imcol, 20 + widthCell * i, 0, 0);
		}
	}

	public void paint(Graphics g) {
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, 240, 320);
		// for (int i = 0; i <= WIDTH + widthCell; i += widthCell) {
		// g.drawLine(i, 0, i, HEIGHT);
		// }
		// for (int i = 0; i <= HEIGHT; i += heightCell) {
		// g.drawLine(0, i, WIDTH, i);
		// }
		// drawBoard(g);
		board.paint(g);
		board.drawValue(g, BoardCaro.getInstance().board, X, O);
		// for (int i = 0; i < caro.ROW; i++) {
		// for (int j = 0; j < caro.COL; j++) {
		// drawCell(caro.getValue(i, j), i, j, g);
		// }
		// }

		if (isExpand) {
			g.drawImage(imExpand, 5, 320 - imExpand.getHeight() - 1, 0);
			move.draw(g);
			clear.draw(g);
			back.draw(g);
		}
		expand.draw(g);
		if (gameOver) {
			g.setColor(250, 70, 250);
			g.setFont(Font.getDefaultFont());
		}

	}

	/**
	 * Called when the pointer is dragged.
	 */
	protected void pointerDragged(int x, int y) {
		if (allowMove) {
			int a, b;
			if (x - currentX < 0)
				a = -2;
			else
				a = 2;
			if (y - currentY < 0)
				b = -2;
			else
				b = 2;
			board.move(a, b);
			repaint();
		}
	}

	/**
	 * Called when the pointer is pressed.
	 */
	protected void pointerPressed(int x, int y) {
		currentX = x;
		currentY = y;
		expand.actionDown(x, y);
		move.actionDown(x, y);
		clear.actionDown(x, y);
		back.actionDown(x, y);
		repaint();
	}

	/**
	 * Called when the pointer is released.
	 */
	protected void pointerReleased(int x, int y) {

		if (gameOver) {
			return;
		}
		if (expand.isPressed || move.isPressed || clear.isPressed
				|| back.isPressed) {
			expand.actionUp(x, y);
			move.actionUp(x, y);
			clear.actionUp(x, y);
			back.actionUp(x, y);
			repaint();
			return;
		}
		if (isExpand) {
			return;
		}
		System.out.println(x + " " + y);

		int real_x = (x + Math.abs(board.x)) / widthCell;
		int real_y = (y + Math.abs(board.y)) / heightCell;
		System.out.println(real_x + " " + real_y);

		if (caro.getValue(real_x, real_y) != 0) {
			warning();
			return;
		}
		caro.setValue(real_x, real_y, 1);
		al.setCurrentEnemy(real_x, real_y);
		if (al.isEnemyWin()) {
			gameOver = true;
			repaint();
			notifyYouWinGame();
			return;
		}
		al.decision();
		if (al.isMeWin()) {
			// gameOverDescription = "0 is winner!";
			gameOver = true;
			repaint();
			notifyMobileWinGame();
			return;
		}
		repaint();
	}

	private void notifyMobileWinGame() {
		// TODO Auto-generated method stub

	}

	private void notifyYouWinGame() {
		// TODO Auto-generated method stub

	}

	private void warning() {
		// TODO Auto-generated method stub

	}

	/**
	 * Called when action should be handled
	 */
	public void commandAction(Command command, Displayable displayable) {
		game.getDisplay().setCurrent(new MenuGame(game));
	}

	public void reset() {
		al.reset();
		caro.reset();
		gameOver = false;
	}

	public boolean inSprite(Sprite s, int x, int y) {
		return x > s.getX() && x < s.getX() + s.getWidth() && y > s.getY()
				&& y < s.getY() + s.getHeight();
	}
}
>>>>>>> .r11
